package com.definity.toolkit.web.servlet;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.definity.toolkit.web.util.WebUtils;

public class LanguageServlet extends HttpServlet {

	private static final long serialVersionUID = 5584504552026828910L;

	public LanguageServlet() {

	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	private void execute(HttpServletRequest request,
			HttpServletResponse response) {
		
		String language = request.getParameter("lang");
		String url = request.getParameter("url");
		Locale locale = WebUtils.getLocale(language);

		request.getSession().setAttribute("defaultLocale", locale);
		
		try {
			response.sendRedirect(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
